/*
 * Copyright 2012 Faculty of Informatics - Masaryk University.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cz.muni.fi.pa165.creatures.dao.impl;

import cz.muni.fi.pa165.creatures.dao.CreatureTypeDAO;
import cz.muni.fi.pa165.creatures.model.CreatureType;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityExistsException;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 * This class represents the implementation of all basic operations upon
 * {@link cz.muni.fi.pa165.creatures.model.CreatureType} instances.
 *
 * @author xvojtis2
 */
@Repository
@Transactional
public class CreatureTypeDAOImpl implements CreatureTypeDAO {
    
    @PersistenceContext
    private EntityManager em;
    
    private static final Logger logger = 
            Logger.getLogger(CreatureTypeDAOImpl.class.getName());

    @Override
    public void create(CreatureType creatureType) {
        try {
            DAOUtils.notNull(creatureType);
            em.persist(creatureType);
        } catch(IllegalArgumentException ex) {
            logger.log(Level.INFO, "Unable to create a creature type. "
                    + "Creature type cannot be a null object.");
        } catch (EntityExistsException ex) {
            logger.log(Level.INFO, "Unable to create a creature type. "
                    + "Creature type to create is already created.");
        }
    }

    @Override
    public void remove(CreatureType creatureType) {
        try {
            DAOUtils.notNull(creatureType);
            CreatureType toRemove = em.find(CreatureType.class, creatureType.getId());
            em.remove(toRemove);
        } catch(IllegalArgumentException ex) {
            logger.log(Level.INFO, "Unable to remove a creature type. "
                    + "Creature type cannot be a null object.");
        }
    }

    @Override
    public void update(CreatureType creatureType) {
        try {
            DAOUtils.notNull(creatureType);
            em.merge(creatureType);
        } catch(IllegalArgumentException ex) {
            logger.log(Level.INFO, "Unable to update a creature type. "
                    + "Creature type cannot be a null object.");
        }
    }

    @Override
    public CreatureType get(Long id) {
        try {
            DAOUtils.notNull(id);
            CreatureType creatureType = em.find(CreatureType.class, id);
            return creatureType;
        } catch(IllegalArgumentException ex) {
            logger.log(Level.INFO, "Unable to get a creature type. " 
                    + "Id of the creature type to get cannot be a null object");
            return null;
        }
    }

    @Override
    public int getCount() {
        Query q = em.createQuery("select count(o) from CreatureType as o");
        return ((Long) q.getSingleResult()).intValue();
    }

    @Override
    public List<CreatureType> getAll() {
        Query q = em.createQuery("from CreatureType");
        return q.getResultList();
    }

}
